package com.project2.controller;

import com.project2.anno.SaveLog;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.bo.AdminLoginBo;
import com.project2.bean.bo.UserUpdateBo;
import com.project2.bean.po.MarketAd;
import com.project2.bean.po.MarketAdmin;
import com.project2.bean.po.MarketNotice;
import com.project2.bean.po.MarketUser;
import com.project2.bean.vo.*;
import com.project2.bean.ResultVo;
import com.project2.service.AdminAuthService;
import com.project2.shiro.MarketToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpSession;
import java.io.Serializable;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @param
 * @author wj
 * @return
 */
@RestController
@RequestMapping("admin")
public class AdminAuthController {
    @Autowired
    AdminAuthService adminAuthService;
    // cbj test
    @Autowired
    HttpServletRequest httpServletRequest;


    @RequestMapping("profile/nnotice")
    public ResultVo nnotice() {
        return ResultVo.ok(0);
    }

    @SaveLog(value = "登录")
    @RequestMapping("auth/login")
    public ResultVo login(@RequestBody AdminLoginBo adminLoginBo, HttpSession session) {
        //获取认证信息
        String password = adminLoginBo.getPassword();
        String username = adminLoginBo.getUsername();
        //获得subject对象执行认证
        Subject subject = SecurityUtils.getSubject();
        //UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        MarketToken admin = new MarketToken(username, password, "admin");
        try {
            subject.login(admin);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //判断是否认证成功
        boolean authenticated = subject.isAuthenticated();
        // cbj 0815修改
        if(authenticated) {

            MarketAdmin marketAdmin = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();

            Serializable id = subject.getSession().getId();

            AuthLoginVo authLoginVo = new AuthLoginVo();
            AuthLoginInfo authLoginInfo = new AuthLoginInfo();
            authLoginInfo.setAvatar(marketAdmin.getAvatar());
            authLoginInfo.setNickName(marketAdmin.getUsername());
            authLoginVo.setToken(((String) id));

            session.setAttribute(authLoginVo.getToken(), marketAdmin);

            session.setAttribute("username", adminLoginBo.getUsername());
//        System.out.println("登录的管理员："+adminLoginBo.getUsername());

            return ResultVo.ok(authLoginVo);
        }
        ResultVo resultVo = new ResultVo();
        resultVo.setErrno(401);
        resultVo.setErrmsg("登录失败，请重试");
        return  resultVo;
    }

    @RequestMapping("auth/info")
    public ResultVo info(String token,HttpServletRequest request) {
        String header = request.getHeader("X-CskaoyanMarket-Admin-Token");
        MarketAdmin attribute = (MarketAdmin) request.getSession().getAttribute(header);
//        MarketAdmin attribute = (MarketAdmin) request.getSession().getAttribute(token);
        AdminInfoVo infoData = adminAuthService.info(attribute);
        return ResultVo.ok(infoData);
    }

    @RequestMapping("dashboard")
    public ResultVo dashboard() {
        DashBoardVo dashBoardVo = adminAuthService.dashboard();
        return ResultVo.ok(dashBoardVo);
    }
    @RequiresPermissions("admin:user:list")
    @RequestMapping("user/list")
    public ResultVo userList(PageBo pageBo, String username, String mobile) {
        UserListVo list = adminAuthService.list(pageBo, username, mobile);
        return ResultVo.ok(list);
    }
    @RequiresPermissions("admin:user:list")
    @RequestMapping("user/detail")
    public ResultVo detail(Integer id) {
        MarketUser user = adminAuthService.detail(id);
        return ResultVo.ok(user);
    }
    @RequiresPermissions("admin:user:list")
    @RequestMapping("user/update")
    public ResultVo update(@RequestBody UserUpdateBo userUpdateBo) {
        int i = adminAuthService.updateUser(userUpdateBo);
        return ResultVo.ok(i);
    }
    @RequiresPermissions("admin:address:list")
    @RequestMapping("address/list")
    public ResultVo addressList(PageBo pageBo, String name, Integer userId) {
        AddressListVo list = adminAuthService.list(pageBo, name, userId);
        return ResultVo.ok(list);
    }
    @RequiresPermissions("admin:collect:list")
    @RequestMapping("collect/list")
    public ResultVo collectList(PageBo pageBo, Integer valueId, Integer userId) {
        CollectListVo list = adminAuthService.list(pageBo, valueId, userId);
        return ResultVo.ok(list);
    }
    @RequiresPermissions("admin:footprint:list")
    @RequestMapping("footprint/list")
    public ResultVo footprintList(PageBo pageBo, Integer goodsId, Integer userId) {
        FootprintListVo list = adminAuthService.footList(pageBo, goodsId, userId);
        return ResultVo.ok(list);
    }
    @RequiresPermissions("admin:history:list")
    @RequestMapping("history/list")
    public ResultVo historyList(PageBo pageBo, String keyword, Integer userId) {
        HistoryListVo list = adminAuthService.historyList(pageBo, keyword, userId);
        return ResultVo.ok(list);
    }
    @RequiresPermissions("admin:feedback:list")
    @RequestMapping("feedback/list")
    public ResultVo feedbackList(PageBo pageBo, String username, Integer id) {
        FeedbackListVo list = adminAuthService.feedbackList(pageBo, username, id);
        return ResultVo.ok(list);
    }

    @SaveLog(value = "退出")
    @RequestMapping("auth/logout")
    public ResultVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
//        String username = (String) httpServletRequest.getSession().getAttribute("username");
//        System.out.println("退出的管理员："+username);
        return ResultVo.ok();
    }
}
